package mephisto;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import mephisto.model.Content;
import mephisto.services.IAuthenticationService;
import mephisto.services.IContentService;
import mephisto.services.IDatabaseService;
import mephisto.services.IGroovyService;
import mephisto.services.IRdfService;
import mephisto.services.IService;
import mephisto.services.IServiceProxy;
import mephisto.services.IStorageService;
import mephisto.services.IWikiModelService;
import mephisto.services.impl.MySqlDatabaseService;

public class Mephisto {

    protected IAuthenticationService authenticationService = null;
    protected IStorageService storageService = null;
    protected IWikiModelService wikiModelService = null;
    protected IRdfService rdfService = null;
    protected IGroovyService groovyService = null;
    protected IServiceProxy proxyService = null;
    protected IContentService contentService = null;
    protected IDatabaseService databaseService = null;
        
    private static Mephisto mephisto = null;
    protected Properties properties = null;

    private Mephisto() throws IOException {
        super();

        InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("mephisto.cfg");
        properties = new Properties();
        properties.load(in);
        in.close();

    }

    public void initializeServices() {
        //wikiModelService = (IWikiModelService) initializeService("service.wikimodel.class");
        //storageService = (IStorageService) initializeService("service.storage.class");
        //proxyService = (IServiceProxy) initializeService("service.proxy.class");
        //contentService = (IContentService) initializeService("service.page.class");
        //groovyService = (IGroovyService) initializeService("service.groovy.class");
        rdfService = (IRdfService) initializeService("service.rdf.class");
        
        String dbuser = get("service.database.user");
        String dbpasswd = get("service.database.password");
        String dburl = get("service.database.url");
        try {
            databaseService = new MySqlDatabaseService(dburl, dbuser, dbpasswd);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public IService initializeService(String classNameProperty) {
        String className = get(classNameProperty);

        if (className == null || className.length() == 0) {
            return null;
        }

        try {
            Class c = Class.forName(className);
            IService service = (IService) c.newInstance();
            String endpointProperty = classNameProperty.substring(0,classNameProperty.indexOf("class"))+"endpoint";
            String endpoint = get(endpointProperty);
            if (endpoint != null) {
                IServiceProxy proxy = (IServiceProxy) service;
                proxy.setEndpoint(endpoint);
                
                
            }
            return service;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    public synchronized static Mephisto getInstance() {
        if (mephisto == null) {
            try {
                mephisto = new Mephisto();
                mephisto.initialize();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return mephisto;
    }

    public void initialize() {
        initializeServices();
    }

    public IStorageService getStorageService() {
        return storageService;
    }

    public IWikiModelService getWikiModelService() {
        return wikiModelService;
    }

    public IRdfService getRdfService() {
        return rdfService;
    }

    public IGroovyService getGroovyService() {
        return groovyService;
    }

    public IAuthenticationService getAuthenticationService() {
        return authenticationService;

    }

    public IServiceProxy getProxyService() {
        return proxyService;
    }

    public String get(String s) {
        return (String) properties.get(s);
    }

    public IContentService getContentService() {
        return contentService;
    }
    
    public IDatabaseService getDatabaseService() {
        return databaseService;
    }

    public static void main(String[] args) {
        Mephisto m = Mephisto.getInstance();
        Content page = new Content("main");
        // page.setContent("hello Mephisto");

        // m.getPageService().savePage(page, null);
        Context context = new Context();
        System.out.println(m.getRdfService());
        // context.setMainPageId("main");
        // context.setUser("arkub");
        // context.setRequestedUri("http://localhost/mephisto/main");
        // context.setRequestedContentDescriptor(page);
        //String content = m.getContentService().getLaidOutRenderedContent("main", context);
        //System.out.println(content);

    }
    
    public String firstUpperCase(String word) {
        if (word == null || word.length() ==1)
            return word;
        String c = word.substring(0,1);
        word = c.toUpperCase()+word.substring(1);
        return word;

    }

}
